package com.t2c.tibetantochinese.search;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.moudle.FiveHundredItem;
import com.t2c.tibetantochinese.moudle.SearchItem;
import com.t2c.tibetantochinese.realmModel.Phrase;
import com.t2c.tibetantochinese.utils.CommonUtil;

import java.io.IOException;
import java.util.List;

import io.realm.RealmResults;

/**
 * Created by zoulu on 16/6/23.
 * 搜索adapter
 */
public class SearchAdapterSimple extends BaseAdapter{

    private Context mContext;
    private View mLastView;
    private int mLastPosition;
    private int mLastVisibility;
    private RealmResults<Phrase> data;
    private MediaPlayer mediaPlayer ;

    public SearchAdapterSimple(Context context, RealmResults<Phrase> items) {
        this.mContext = context;
        mediaPlayer = new MediaPlayer();
        mLastPosition = -1;
        this.data = items;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null ) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.search_item, null);
            holder =new Holder();
            holder.tv_chinese = (TextView)convertView.findViewById(R.id.tv_chinese);
            holder.iv_sound1 = (ImageView)convertView.findViewById(R.id.iv_sound1);
            holder.iv_sound2 = (ImageView)convertView.findViewById(R.id.iv_sound2);
            holder.tv_tibetan = (TextView)convertView.findViewById(R.id.tv_tibetan);
            holder.iv_out = (ImageView)convertView.findViewById(R.id.iv_out);
            holder.iv_collect = (ImageView)convertView.findViewById(R.id.iv_collect);
            holder.tv_word = (TextView) convertView.findViewById(R.id.tv_word);
            holder.tv_sentence = (TextView) convertView.findViewById(R.id.tv_sentence);
            holder.re_hind = (RelativeLayout) convertView.findViewById(R.id.re_hind);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        if(mLastPosition == position){
            holder.re_hind.setVisibility(mLastVisibility);
        }else{
            holder.re_hind.setVisibility(View.GONE);
        }
        final View finalConvertView = convertView;
        holder.iv_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeREVisable(finalConvertView,position);
            }
        });

        holder.tv_chinese.setText(data.get(position).getContent());
        holder.tv_tibetan.setTypeface(CommonUtil.getTibetanType(mContext));
        holder.tv_tibetan.setText(data.get(position).getTibetcontent());
        holder.tv_word.setText(data.get(position).getCategory().getName());
        holder.tv_sentence.setTypeface(CommonUtil.getTibetanType(mContext));
        holder.tv_sentence.setText(data.get(position).getCategory().getTibet());

        holder.iv_sound1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playAudio(mContext,position,1);
            }
        });

        holder.iv_sound2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playAudio(mContext,position,2);
            }
        });

        return convertView;
    }


    public void changeREVisable(View view,int position) {
        if(mLastView != null && mLastPosition != position ) {
            Holder holder = (Holder) mLastView.getTag();
            switch(holder.re_hind.getVisibility()) {
                case View.VISIBLE:
                    holder.re_hind.setVisibility(View.GONE);
                    mLastVisibility = View.GONE;
                    break;
                default :
                    break;
            }
        }
        mLastPosition = position;
        mLastView = view;
        Holder holder = (Holder) view.getTag();
        switch(holder.re_hind.getVisibility()) {
            case View.GONE:
                holder.re_hind.setVisibility(View.VISIBLE);
                mLastVisibility = View.VISIBLE;
                break;
            case View.VISIBLE:
                holder.re_hind.setVisibility(View.GONE);
                mLastVisibility = View.GONE;
                break;
        }
    }

    private class Holder{
        TextView tv_chinese;
        ImageView iv_sound1;
        TextView tv_tibetan;
        ImageView iv_sound2;
        ImageView iv_out;
        ImageView iv_collect;
        RelativeLayout re_hind;
        TextView tv_word;
        TextView tv_sentence;
    }

    /**
     * 播放语音
     * @param type 1 汉语  2 藏语
     */
    private void playAudio(Context context,int pos , int type){

        try {
            mediaPlayer.reset();
            AssetFileDescriptor descriptor = null;
            if(type == 1) {
                descriptor = CommonUtil.getAudioPath(context,data.get(pos).getAudio().getAudiopath());
            }else if(type == 2){
                descriptor = CommonUtil.getAudioPath(context,data.get(pos).getAudio().getTibetaudiopath());
            }
            assert descriptor != null;
            mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
